from models_final import get_asr, get_llm,get_qwen
from prompts_final import summary_prompt, fanyi_prompt
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import gradio as gr

# 初始化模型
asr_model = get_asr()
summary_model = get_llm()
fanyi_model = get_qwen()

# 全局变量用于存储转录结果
transcription_result = None
# 语音识别
def get_transcription(audio_file):
    global transcription_result
    try:
        # 读取音频文件
        with open(audio_file.name, "rb") as f:
            transcription_result = asr_model.transcriptions(f.read())["text"]
        return transcription_result
    except Exception as e:
        return f"处理出错: {str(e)}"
# 会议纪要
def get_summary():
    global transcription_result
    if transcription_result is None:
        return "当前语音识别未结束，请先进行语音识别"
    try:
        chain = summary_prompt | summary_model | StrOutputParser()
        summary = chain.invoke({"text": transcription_result})
        return summary
    except Exception as e:
        return f"处理出错: {str(e)}"


# 在线翻译
# 在线翻译
def get_translation(language):
    global transcription_result
    if transcription_result is None:
        return "当前语音识别未结束，请先进行语音识别"
    try:
        chain = fanyi_prompt | fanyi_model | StrOutputParser()
        fanyitext = chain.invoke(
            {"target_language": language, "text": transcription_result}
        )
        return fanyitext
    except Exception as e:
        return f"翻译出错: {str(e)}"


# 创建Gradio界面
# with gr.Blocks(
#     theme="soft",  # 采用柔和主题
#     title="智能会议纪要生成器",
#     css="""
#     .gradio-container {max-width: 1200px !important}
#     .panel {border: 1px solid #e0e0e0; border-radius: 8px; padding: 15px}
#     button {transition: all 0.3s ease}
#     button:hover {opacity: 0.8; transform: translateY(-2px)}
#     footer {visibility: hidden !important}
#     /* 新增滚动条控制 */
#     .highlight-box {
#     border: 2px solid #e0e0e0 !important;
#     border-radius: 8px;
#     max-height: 300px !important;  /* 设置最大高度 */
#     overflow-y: auto !important;  /* 启用垂直滚动条 */
#     }
#     /* 文本域高度锁定 */
#     .highlight-box textarea {
#     max-height: 280px !important;  /* 略小于容器高度 */
#     min-height: 120px !important;  /* 保持初始高度 */
#     }""",
#     ) as interface:

#     # 标题区域
#     gr.Markdown(
#         """
#     <div style="text-align:center">
#     <h1 style="color:#2c3e50">📝 智能会议纪要生成器</h1>
#     <p style="color:#7f8c8d">上传会议录音文件（MP3/WAV），自动生成结构化会议摘要</p>
#     </div>
#     """
#     )

#     # 核心功能区域
#     with gr.Row(variant="panel"):
#         # 左侧上传区域
#         with gr.Column(scale=1):
#             audio_input = gr.File(
#                 label="上传会议录音\n（支持MP3/WAV格式，文件大小<50MB）",
#                 file_types=[".mp3", ".wav"],
#                 elem_classes="panel",
#             )

#         # 中间处理区域
#         with gr.Column(scale=2):
#             transcription_output = gr.Textbox(
#                 label="语音识别结果",
#                 lines=5,
#                 placeholder="识别文本将在此显示...",
#                 show_copy_button=True,
#                 elem_classes="highlight-box",
#             )
#             summary_output = gr.Textbox(
#                 label="会议摘要",
#                 lines=10,
#                 placeholder="结构化摘要将在此生成...",
#                 show_copy_button=True,
#                 elem_classes="highlight-box",
#             )

#     # 操作按钮区域
#     with gr.Row():
#         transcribe_button = gr.Button(
#             "🎤 开始识别", variant="primary", size="sm", min_width=120
#         )
#         summary_button = gr.Button(
#             "✨ 生成纪要", variant="primary", size="sm", min_width=120
#         )

#     # 翻译功能区域
#     with gr.Row(variant="panel"):
#         with gr.Column(scale=1):
#             lang_dropdown = gr.Dropdown(
#                 ["英语", "日语", "法语"],
#                 label="翻译目标语言",
#                 interactive=True,
#             )
#         with gr.Column(scale=2):
#             fanyi_output = gr.Textbox(
#                 label="翻译结果",
#                 lines=5,
#                 show_copy_button=True,
#                 interactive=False,
#                 elem_classes="highlight-box",
#             )
#         fanyi_btn = gr.Button(
#             "🌐 立即翻译", variant="secondary", size="sm", min_width=120
#         )

#     # 使用说明
#     with gr.Accordion("使用指南", open=False):
#         gr.Markdown(
#             """
#         1. ​**上传录音**：点击上传按钮选择清晰的会议录音文件
#         2. ​**语音识别**：点击识别按钮获取文字稿（需等待处理完成）
#         3. ​**生成摘要**：文字识别完成后点击生成结构化会议纪要
#         4. ​**翻译功能**：选择目标语言后点击翻译按钮获取多语言版本
#         """
#         )

    # 功能绑定（保持不变）
    # transcribe_button.click(
    #     fn=get_transcription, inputs=audio_input, outputs=transcription_output
    # )
    # summary_button.click(fn=get_summary, inputs=None, outputs=summary_output)
    # fanyi_btn.click(fn=get_translation, inputs=lang_dropdown, outputs=fanyi_output)

# if __name__ == "__main__":
#     interface.launch(server_port=8080)
